package cn.team11.sds.dayi.ble;

import java.util.Map;

import cn.team11.sds.dayi.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;


import com.aliyun.iot.smurfs.base.OperateScene;
import com.aliyun.iot.smurfs.listener.SmurfsOperateSceneDelegate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;




/*

//使用smurfs蓝牙库需要在使用前首先preload，整个应用程序只需要preload一次
Smurfs.preload(getApplicationContext());

Smurfs OperateScene说明：

        //使用mac地址初始化
        this.operateScene = new OperateScene(mac);
        //设置委托，需要实现SmurfsOperateSceneDelegate接口
        operateScene.setDelegate(this);
        //连接设备
        operateScene.enter();
        //退出断开连接调用exit方法
        opreateScene.exit();

        //连接成功后可以使用的才做
        opreateScene.write(uuid,baseData);
        opreateScene.read(uuid);
        opreateScene.notify(uuid);

 */

public class BLEOperateActivity extends Activity  implements SmurfsOperateSceneDelegate {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        configListview();
        configBLE();
    }


    ListView listView;
    List<Map<String, Object>> data;
    SimpleAdapter adapter;
    public String mac;
    OperateScene operateScene;
    static String TAG = "doa";


    private void configBLE(){
        mac = getIntent().getStringExtra("mac");
        this.operateScene = new OperateScene(mac);
        //设置委托
        operateScene.setDelegate(this);
        //连接设备
        operateScene.enter();

        //显示消息
        insertMsg("开始连接","aa");
    }

    private void configListview(){

        this.data  = new ArrayList<Map<String, Object>>();

        adapter = new SimpleAdapter(this,this.data,R.layout.blediscovery_listview,
            new String[]{"mac","info"},
            new int[]{R.id.mac,R.id.info});
        //        setListAdapter(adapter);

        listView = new ListView(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //                insertMsg("write","{0x1e,0x07,0x12,0x11,0x01,0x02,0x03}");
                byte[] data =  {0x1e,0x07,0x12,0x11,0x01,0x02,0x03};
                operateScene.write("fff2",data);

            }
        });
        setContentView(listView);
    }


    //iot 蓝牙虚拟设备测试
    public void aliyun_iot_bt_virtual_device() {
//        byte[] data =  {0x1e,0x07,0x12,0x11,0x01,0x02,0x03};
//        operateScene.openNotify("fff3");
//        operateScene.read("fff1");
//        operateScene.write("fff2",data);
        throw new RuntimeException("！！！！阿里api接口修改，此部分暂未修复！！！！");
    }

    @Override
    public void onReady() {

        Log.d(TAG,"onReady");
        //显示消息
        insertMsg("onReady","");

        aliyun_iot_bt_virtual_device();
    }

    @Override
    public void onReceived(String uuid, byte[] data) {
        Log.d(TAG,"onReceived");
        //显示消息
        insertMsg("onReceived",data.toString());
    }

    @Override
    public void onMessage(String event, String uuid, String data) {
        Log.d(TAG,"onMessage");
        //        insertMsg("onMessage","");

        //显示消息
        insertMsg(event,"uuid:"+uuid + "| data:" +data);
    }

    @Override
    public void onLog(String msg) {
        Log.d(TAG,"onLog");        //显示消息
    }


    private void insertMsg(final String event, final String msg){
        //
        Handler mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("mac",event);
                map.put("info",msg);
                data.add(map);
                adapter.notifyDataSetChanged();
                listView.setSelection(adapter.getCount()-1);
            }
        });

    }


}
